<?php

namespace app\middleware;

use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\response\Json;

class LoginCheck
{
    /**
     * @param 
     */
    public function handle($request, \Closure $next)
    {       
        try{
            $jwt=$request->header('Authorization');
            if(!$jwt){
                throw new Exception("请登录");
            }
            $key="xuri";            
            $jwt=str_replace("Bearer ", "", $jwt);    
            $key = new Key($key, 'HS256');   
            $decoded = JWT::decode($jwt, $key);
            $request->userInfo = $decoded->data;
            
        }catch(Exception $e){
            return Json(['code'=>0,'msg'=>$e->getMessage()]);           
        } 
        return $next($request);
    }
}